﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Estimate.Common.Estimate;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Entity;
using Estimate.Common.DataFileFinder;
using Estimate.Reports;

namespace EstimateService
{
    /// <summary>
    /// 气温图表数据接口
    /// </summary>
    public class TempDiagramReportHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //string tables = context.Request.QueryString["tables"] != null ? context.Request.QueryString["tables"].ToString() : "";
            string date = context.Request.QueryString["date"] != null ? context.Request.QueryString["date"].ToString() : "";
            string station = context.Request.QueryString["station"] != null ? context.Request.QueryString["station"].ToString() : "";
            string sendpoint = context.Request.QueryString["sendpoint"] != null ? context.Request.QueryString["sendpoint"].ToString() : "";
            string span = context.Request.QueryString["span"] != null ? context.Request.QueryString["span"].ToString() : "";
            string tid = context.Request.QueryString["tid"] != null ? context.Request.QueryString["tid"].ToString() : "";

            //获取回调函数名
            string callback = context.Request.QueryString["callback"];
            //json数据
            string json = GetTempDiagramReportData(date, station, sendpoint, span, tid);

            context.Response.ContentType = "application/json";
            //输出：回调函数名(json数据)
            context.Response.Write(callback + "(" + json + ")");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public string GetTempDiagramReportData(string date, string station, string sendpoint, string span, string tid)
        {
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
            string tablestr = ConfigurationManager.AppSettings["TempDiagramTables"];

            Dictionary<string, string> tables = new Dictionary<string, string>();
            foreach (string s in tablestr.Split('|'))
            {
                string[] strs = s.Split(';');
                tables.Add(strs[0], strs[1]);
            }

             DateTime dateTime = DateTime.Parse(date);
             ForeSendpointType sendPoint = (ForeSendpointType)Enum.Parse(typeof(ForeSendpointType), sendpoint);
             //ForeSpanType foreSpan = (ForeSpanType)Enum.Parse(typeof(ForeSpanType), span);

             TempDiagramReport tempDiagramReport = new TempDiagramReport(connectionString, tid);
             return tempDiagramReport.GetReportDataString(tables, dateTime, span, station, sendPoint);
        }

    }
}